package com.znufe.exam.utility;

import org.springframework.web.multipart.MultipartFile;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;

import java.io.*;
import java.util.Arrays;
import java.util.Iterator;

public class VideoUtil {
    //视频最大1024M
    private static long video_upload_maxsize = 1024 * 1024 * 1024;

    //图片最大100M
    private static long photo_upload_maxsize = 100 * 1024 * 1024;

    //视频允许格式
    private static String[] videoAllowFiles = {".mp4",".avi",".flv",".mov",".wmv"};

    //图片允许格式
    private static String[] photoAllowFiles = {".jpg",".jpeg",".png"};

    public static boolean checkVideoSize(MultipartFile file){
        if(file.getSize() <= video_upload_maxsize){
            return true;
        }else {
            return false;
        }
    }

    public static boolean checkPhotoSize(MultipartFile file){
        if(file.getSize() <= photo_upload_maxsize){
            return true;
        }else {
            return false;
        }
    }

    public static boolean checkVideoType(String fileName){
        Iterator<String> type = Arrays.asList(videoAllowFiles).iterator();
        while (type.hasNext()){
            String ext = type.next();
            if (fileName.toLowerCase().endsWith(ext)){
                return true;
            }
        }
        return false;
    }

    public static boolean checkPhotoType(String fileName){
        Iterator<String> type = Arrays.asList(photoAllowFiles).iterator();
        while (type.hasNext()){
            String ext = type.next();
            if (fileName.toLowerCase().endsWith(ext)){
                return true;
            }
        }
        return false;
    }


    public static String getVideoDuration(File file){
        String length = "";
        try {
            Encoder encoder = new Encoder();
            MultimediaInfo info = encoder.getInfo(file);
            long ls = info.getDuration()/1000;
            file.delete();
            int hour = (int)(ls/3600);
            int minute = (int)(ls%3600)/60;
            int second = (int)(ls-hour*3600-minute*60);
            length = hour+":"+minute+":"+second;
            return length;
        } catch (Exception e) {
            e.printStackTrace();
            return length;
        }
    }

    public static String getVideoDurationNotDelete(File file){
        String length = "";
        try {
            Encoder encoder = new Encoder();
            MultimediaInfo info = encoder.getInfo(file);
            long ls = info.getDuration()/1000;
            int hour = (int)(ls/3600);
            int minute = (int)(ls%3600)/60;
            int second = (int)(ls-hour*3600-minute*60);
            length = hour+":"+minute+":"+second;
            return length;
        } catch (Exception e) {
            e.printStackTrace();
            return length;
        }
    }


    public static File multipartFileToFile(MultipartFile file) throws Exception {
        File toFile = null;
        if (file.equals("") || file.getSize() <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            ins = file.getInputStream();
            toFile = new File(file.getOriginalFilename());
            inputStreamToFile(ins, toFile);
            ins.close();
        }
        return toFile;
    }

    //获取流文件
    public static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
